package com.fcgzs.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.fcgzs.item.feign.SkuFeign;
import com.xpand.starter.canal.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Auther: ljj
 * @Description:   Canal监听同步数据监听类
 */
@CanalEventListener  //开启Canal事件监听
public class CanalDataEventListener {


    @Autowired
    private SkuFeign skuFeign;

    /**
     *   getBeforeColumnsList()   事件变化前的数据 修改  删除
     *
     *   getAfterColumnsList()   事件变化后的数据  修改  新增
     * @param eventType
     * @param rowData
     */
    //监听新增
    /*@InsertListenPoint
    public void onEventInsert(CanalEntry.EventType eventType,CanalEntry.RowData rowData){
        for (CanalEntry.Column column : rowData.getAfterColumnsList()) {
            System.out.println("新增后的数据："+column.getName()+"--"+column.getValue());
        }
    }*/


    //监听修改
    /*@UpdateListenPoint
    public void onEventUpdate(CanalEntry.EventType eventType,CanalEntry.RowData rowData){
        for (CanalEntry.Column column : rowData.getBeforeColumnsList()) {
            System.out.println("修改前的数据:"+column.getName()+"--"+column.getValue());
        }

        for (CanalEntry.Column column : rowData.getAfterColumnsList()) {
            System.out.println("修改后的数据："+column.getName()+"--"+column.getValue());
        }
    }*/

    //监听删除
   /* @DeleteListenPoint
    public void onEventDelete(CanalEntry.RowData rowData){
        for (CanalEntry.Column column : rowData.getBeforeColumnsList()) {
            System.out.println("删除前的数据:"+column.getName()+"--"+column.getValue());
        }
    }*/

    //自定义监听
    @ListenPoint(destination = "example", schema = "dongyimaidb_0622", table = {"tb_content_category", "tb_content"}, eventType = {CanalEntry.EventType.INSERT, CanalEntry.EventType.UPDATE, CanalEntry.EventType.DELETE})
    public void onEventCustomUpdate(CanalEntry.RowData rowData){
        System.out.println("自定义监听");
        for (CanalEntry.Column column : rowData.getAfterColumnsList()) {
            System.out.println("自定义新增后的数据："+column.getName()+"--"+column.getValue());
        }
    }

    //监听商品表的变化,调用Itemweb的服务
    @ListenPoint(destination = "example",schema = "dongyimaidb_0622",table = {"tb_goods"},eventType = {CanalEntry.EventType.INSERT, CanalEntry.EventType.UPDATE, CanalEntry.EventType.DELETE})
    public void onEventCustomSpu(CanalEntry.EventType eventType,CanalEntry.RowData rowData){
        System.out.println("监控到商品表发生变化");
        //判断数据库中的操作是否删除
        if(eventType.equals(CanalEntry.EventType.DELETE)){
            //执行本地删除文件
        }else{
            for (CanalEntry.Column column : rowData.getAfterColumnsList()) {
                System.out.println("更新的数据："+column.getName()+"---"+column.getValue());
                if(column.getName().equals("id")){
                        //执行远程服务调用  注意：调用ID所对应的商品  一定要三张表都有数据
                    skuFeign.createItemHtml(Long.parseLong(column.getValue()));
                    break;
                }
            }
        }
    }



}
